#pragma once
#include "SCRambl.h"

class CConsoleCommand
{
	void	(*Func)(const char *[], char);
	char	Command[24];
public:
	static CConsoleCommand Commands[32];
	static int s_nNumCommands;

	static void Add(const char *szCmd, void(*func)(const char *[], char))
	{
		szncpy(Commands[s_nNumCommands].Command, szCmd, sizeof(Commands[s_nNumCommands].Command));
		Commands[s_nNumCommands++].Func = func;
	}
	
	static void ProcessCommand(const char szCmd[512])
	{
		char cmd[512];
		const char * p[12];
		int n=0, b=0;

		szncpy(cmd, szCmd, sizeof(cmd));

		if(!IsSpace(*cmd))
		{
			strtok(cmd, " \t\n");

			while((p[n] = strtok(NULL, "\"\t\n")) && n < sizeof(p))
				n++;

			for(int i=0; i<sizeof(Commands); i++)
			{
				CConsoleCommand &v = Commands[i];
				if(v.Func && !strnicmp(v.Command, cmd, sizeof(v.Command)))
				{
					Print("");
					if(p[0] && (*p[0] == 'h' || *p[0] == '?')) v.Func(p, true);
					else v.Func(p, false);
					Print("");
					return;
				}
			}

			Printf("ERROR: Unknown command (%s)", cmd);
		}
	}
};

int						CConsoleCommand::s_nNumCommands;
CConsoleCommand			CConsoleCommand::Commands[];